home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / gblanker36_src.lha / GSource.lha / GSource / Blankers / Text / blank.c next >
Encoding:
C/C++ Source or Header  |  1994-11-15  |  5.5 KB  |  243 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <hardware/custom.h>
  11. #include <string.h>
  12. #include <clib/diskfont_protos.h>
  13. #include <pragmas/diskfont_pragmas.h>
  14. #include "/includes.h"
  15.  
  16. #define RAND( base, offset ) (( LONG )( RangeRand( base ) + offset ))
  17.  
  18. #ifndef min
  19. #define min( x, y ) ( (x) < (y) ? (x) : (y) )
  20. #endif
  21.  
  22. #define STRING 0
  23. #define FONT   2
  24. #define COLORS 4
  25. #define DELAY  6
  26. #define MODE   8
  27.  
  28. typedef struct _mPoint
  29. {
  30.     LONG x;
  31.     LONG y;
  32. } mPoint;
  33.  
  34. typedef struct _Line
  35. {
  36.     BYTE ln_Text[128];
  37.     LONG ln_Length;
  38.     LONG ln_Offset;
  39. }
  40. Line;
  41.  
  42. extern __far struct Custom custom;
  43.  
  44. LONG vals[] = {
  45.     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
  46.     0x0D, 0x0E, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06,
  47.     0x05, 0x04, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  48.     0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  49.     };
  50.  
  51. VOID Defaults( PrefObject *Prefs )
  52. {
  53.     strcpy( Prefs[STRING].po_Value, "Every good boy deserves fudge." );
  54.     Prefs[FONT].po_Attr.ta_YSize = 75;
  55.     strcpy( Prefs[FONT].po_Name, "topaz.font" );
  56.     Prefs[COLORS].po_Active = 0;
  57.     Prefs[DELAY].po_Level = 60;
  58.     Prefs[MODE].po_ModeID = getTopScreenMode();
  59. }
  60.  
  61. LONG FillLine( Line *Item, STRPTR String, LONG Pos, struct Screen *Scr )
  62. {
  63.     LONG i, End = 0;
  64.     STRPTR Ptr;
  65.  
  66.     while( String[Pos] == ' ' )
  67.         Pos++;
  68.     for( i = 0, Ptr = String + Pos;; Ptr++, i++ )
  69.     {
  70.         if( *Ptr == ' ' )
  71.             End = i;
  72.         else if(( *Ptr == '\n' )|| !*Ptr )
  73.         {
  74.             End = i;
  75.             break;
  76.         }
  77.         Item->ln_Text[i] = *Ptr;
  78.         if( TextLength( &Scr->RastPort, Item->ln_Text, i+1 ) >= Scr->Width )
  79.             break;
  80.     }
  81.     if(( *Ptr == ' ' )||( *Ptr == '\n' )|| !End )
  82.         End = i;
  83.     Item->ln_Text[End] = '\0';
  84.     Item->ln_Length = TextLength( &Scr->RastPort, Item->ln_Text,
  85.                                  strlen( Item->ln_Text ));
  86.     
  87.     return End + Pos;
  88. }
  89.  
  90. Line *CalculateExtent( STRPTR String, struct Screen *Scr, LONG *MaxWid,
  91.                       LONG *NumLines )
  92. {
  93.     LONG TxWidth = Scr->RastPort.TxWidth, TxHeight = Scr->RastPort.TxHeight;
  94.     LONG Lines, Length = strlen( String ), LinePos, i;
  95.     Line *LineArray;
  96.     
  97.     Lines = min( Scr->Height / TxHeight, Length * TxWidth / Scr->Width + 2 );
  98.     
  99.     if( LineArray = AllocVec( sizeof( Line ) * Lines, MEMF_CLEAR ))
  100.     {
  101.         LinePos = i = *MaxWid = *NumLines = 0;
  102.         do
  103.         {
  104.             LinePos = FillLine( &LineArray[i], String, LinePos, Scr );
  105.             if( LineArray[i].ln_Length > *MaxWid )
  106.                 *MaxWid = LineArray[i].ln_Length;
  107.             *NumLines += 1;
  108.         }
  109.         while( ++i < Lines && LinePos < Length );
  110.         for( i = 0; i < *NumLines; i++ )
  111.             LineArray[i].ln_Offset = ( *MaxWid - LineArray[i].ln_Length ) / 2;
  112.     }
  113.  
  114.     return LineArray;
  115. }
  116.  
  117. LONG Blank( PrefObject *Prefs )
  118. {
  119.     LONG Wid, Hei, dx = -1, dy = -1, count = 0, MaxWid, Lns;
  120.     LONG i, c1 = 0, c2 = 14, c3 = 28, RetVal = OK;
  121.     mPoint new, old, size, min, max;
  122.     struct Library *DiskfontBase;
  123.     struct TextFont *font;
  124.     struct Screen *TextScr;
  125.     struct RastPort *Rast;
  126.     struct ViewPort *View;
  127.     struct Window *Wnd;
  128.     Line *Lines = 0L;
  129.  
  130.     if(!( DiskfontBase = OpenLibrary( "diskfont.library", 37 )))
  131.         return FAILED;
  132.     
  133.     Prefs[FONT].po_Attr.ta_Name = Prefs[FONT].po_Name;
  134.     font = OpenDiskFont( &Prefs[FONT].po_Attr );
  135.     if( !font )
  136.         Prefs[FONT].po_Attr.ta_Name = "topaz.font";
  137.     
  138.     TextScr = OpenScreenTags( 0l, SA_DisplayID, Prefs[MODE].po_ModeID,
  139.                              SA_Depth, 1, SA_Quiet, TRUE, SA_Behind, TRUE,
  140.                              SA_Overscan, OSCAN_STANDARD,
  141.                              SA_Font, &( Prefs[FONT].po_Attr ), TAG_DONE );
  142.     if( !TextScr )
  143.         goto JAIL;
  144.  
  145.     Rast = &( TextScr->RastPort );
  146.     View = &( TextScr->ViewPort );
  147.     Wid = TextScr->Width;
  148.     Hei = TextScr->Height;
  149.     
  150.     Lines = CalculateExtent( Prefs[STRING].po_Value, TextScr, &MaxWid, &Lns );
  151.     if( !Lines )
  152.         goto JAIL;
  153.     
  154.     SetRGB4( View, 0, 0L, 0L, 0L );
  155.     switch( Prefs[COLORS].po_Active )
  156.     {
  157.     case 0:
  158.         SetRGB4( View, 1, vals[c1], vals[c2], vals[c3] );
  159.         break;
  160.     case 1:
  161.         SetRGB4( View, 1, RAND( 10, 5 ), RAND( 10, 5 ), RAND( 10, 5 ));
  162.         break;
  163.     case 3:
  164.         setCopperList( Hei, 1, View, &custom );
  165.     case 2:
  166.         SetRGB4( View, 1, 15, 15, 15 );
  167.         break;
  168.     }        
  169.     
  170.     size.x = MaxWid + 2;
  171.     size.y = Lns * Prefs[FONT].po_Attr.ta_YSize + 2;
  172.     new.x = old.x = ( Wid - size.x ) / 2;
  173.     new.y = old.y = ( Hei - size.y ) / 2;
  174.     
  175.     min.x = 0;
  176.     min.y = 0;
  177.     max.x = Wid - size.x - 3;
  178.     max.y = Hei - size.y - 3;
  179.     
  180.     SetAPen( Rast, 1 );
  181.     for( i = 0; i < Lns; i++ )
  182.     {
  183.         Move( Rast, old.x + Lines[i].ln_Offset + 1,
  184.              old.y + i * font->tf_YSize + font->tf_Baseline + 1 );
  185.         Text( Rast, Lines[i].ln_Text, strlen( Lines[i].ln_Text ));
  186.     }
  187.     
  188.     Wnd = BlankMousePointer( TextScr );
  189.     ScreenToFront( TextScr );
  190.     
  191.     while( RetVal == OK )
  192.     {
  193.         WaitTOF();
  194.         
  195.         if(!( count++ % 60 ))
  196.             ScreenToFront( TextScr );
  197.         
  198.         if( !Prefs[COLORS].po_Active && !( count % 10 ))
  199.         {
  200.             c1 = ++c1 % 42;
  201.             c2 = ++c2 % 42;
  202.             c3 = ++c3 % 42;
  203.             SetRGB4( View, 1, vals[c1], vals[c2], vals[c3] );
  204.         }
  205.         
  206.         if( !Prefs[DELAY].po_Level || !( count % Prefs[DELAY].po_Level ))
  207.         {
  208.             new.x += dx;
  209.             new.y += dy;
  210.             
  211.             if( new.x < min.x ) { new.x = min.x; dx = 1; }
  212.             else if( new.x > max.x ) { new.x = max.x; dx = -1; }
  213.             
  214.             if( new.y < min.y ) { new.y = min.y; dy = 1; }
  215.             else if( new.y > max.y ) { new.y = max.y; dy = -1; }
  216.             
  217.             BltBitMapRastPort( Rast->BitMap, old.x, old.y, Rast, new.x,
  218.                               new.y, size.x, size.y, 0xC0 );
  219.             
  220.             old = new;
  221.         }
  222.         RetVal = ContinueBlanking();
  223.     }
  224.     UnblankMousePointer( Wnd );
  225.     
  226.     if( Prefs[COLORS].po_Active == 3 )
  227.         clearCopperList( View );
  228.  
  229.  JAIL:    
  230.     if( Lines )
  231.         FreeVec( Lines );
  232.  
  233.     if( TextScr )
  234.         CloseScreen( TextScr );
  235.  
  236.     if( font )
  237.         CloseFont( font );
  238.  
  239.     CloseLibrary( DiskfontBase );
  240.     
  241.     return RetVal;
  242. }
  243.